package com.fitbet.admin.web.controller.bet;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fitbet.admin.service.bet.UserBetService;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetStatus;
import com.fitbet.entity.bet.UserBetSupport;

/**
 * 挑战审核
 * @author sjh
 *
 */

@Controller
@RequestMapping("/userbets")
@MenuSetting("menu-bet") 
public class UserBetController {

	public static final Logger logger = LoggerFactory.getLogger(UserBetController.class);
	
	@Autowired
	private UserBetService userBetService;
	
	
	
	
	/**
	 * 
	 * @param status
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@MenuSetting("submenu-challenge-audit")
	public ModelAndView list(@RequestParam(name = "status", defaultValue = "OPEN") UserBetStatus status, Page page, HttpServletRequest request) {
		
		
		ModelAndView modelAndView = new ModelAndView("bet/user-bet/user-bet-list");
		
		int totals = userBetService.count(status);
		
		modelAndView.addObject("status", status);
		
		if(totals > 0) {
			
			List<UserBet> userBets = userBetService.list(status, page.with(totals));
		
			modelAndView.addObject("page", page);
			
			modelAndView.addObject("userBets", userBets);
		
		}
		
		modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
		
		return modelAndView;
	}
	
	/**
	 * 查看视频
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/video", method = RequestMethod.GET)
	public ModelAndView viewVideo(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("bet/user-bet/user-bet-view-video");
		
		UserBet userBet = userBetService.load(UserBet.class, id);
		
		modelAndView.addObject("userBet", userBet);
		
		return modelAndView;
		
	}
	
	/**
	 * 开启挑战的认证
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/auth", method = RequestMethod.GET)
	@MenuSetting("submenu-challenge-audit")
	public ModelAndView auth(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("bet/user-bet/user-bet-auth");
		UserBet userBet = userBetService.load(UserBet.class, id);
		
		modelAndView.addObject("userBet", userBet);
		return modelAndView;
	}
	
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/auth", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onAuth(@PathVariable("id") Long id, @RequestParam("passed") boolean passed) {
		
		HttpResult httpResult = new HttpResult();
		
		try {
			
			userBetService.auth(id, passed);
			
			httpResult.success().message("操作成功");
			
		}catch (BusinessException  e) {
			logger.error("#onPay() occurs error", e);
        	
 			httpResult.fail(e.getCode()).message(e.getMessage());
		} catch (Exception e) {

			logger.error("onAuth occurs error", e);
		
			httpResult.fail("500").message("操作失败");
		
		}
		
		return httpResult;
		
	}
	
	/**
     * 结束挑战的认证
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/close", method = RequestMethod.GET)
    @MenuSetting("submenu-challenge-audit")
    public ModelAndView close(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("bet/user-bet/user-bet-close");
        UserBet userBet = userBetService.load(UserBet.class, id);
        modelAndView.addObject("userBet", userBet);
        return modelAndView;
    }
    
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/close", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onClose(@PathVariable("id") Long id,
    						  @RequestParam(name = "reload", required = false) Boolean reload,
    						  @RequestParam("success") boolean success) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
			
			userBetService.close(id, reload, success);
			
			httpResult.success().message("操作成功");
			
		} catch (Exception e) {

			logger.error("onClose occurs error", e);
			
			httpResult.fail("500").message("操作失败");
		
		}
		
        
        return httpResult;
        
    }
    
	
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
    @MenuSetting("submenu-challenge-audit")
    public ModelAndView view(@PathVariable("id") Long id) {
    	ModelAndView modelAndView = new ModelAndView("bet/user-bet/user-bet-view");
    	
    	UserBet userBet = userBetService.load(UserBet.class, id);
    	
    	List<UserBetSupport> userBetSupport = userBetService.listById(id);
    	
    	modelAndView.addObject("userBet", userBet);
    	modelAndView.addObject("userBetSupport", userBetSupport);
    	return modelAndView;
    }
    
}
